package com.xingchen.feima.star.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingchen.feima.common.utils.R;
import com.xingchen.feima.star.entity.MinimumCoolingTime;
import com.xingchen.feima.star.entity.Range;
import com.xingchen.feima.star.entity.ShippingTime;
import com.xingchen.feima.star.service.MinimumCoolingTimeService;
import com.xingchen.feima.star.service.RangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 最小冷却时间表 前端控制器
 * </p>
 *
 * @author yangxc
 * @since 2025-02-16
 */
@Api(tags = "最小冷却时间管理")
@RestController
@RequestMapping("/star/minimumCoolingTime")
public class MinimumCoolingTimeController {

    @Autowired
    private MinimumCoolingTimeService minimumCoolingTimeService;

    @Autowired
    private RangeService rangeService;

    @ApiOperation(value = "保存最小冷却时间矩阵")
    @PostMapping("/saveMinimumCoolingTimeMatrix")
    public R saveMinimumCoolingTimeMatrix(
            @ApiParam(name = "minimumCoolingTimeMatrix", value = "最小冷却时间矩阵", required = true)
            @RequestBody List<List<MinimumCoolingTime>> minimumCoolingTimeMatrix){

        List<MinimumCoolingTime> minimumCoolingTimeListForSaveOrUpdate = new ArrayList<>();
        for (List<MinimumCoolingTime> minimumCoolingTimeRow : minimumCoolingTimeMatrix) {
            minimumCoolingTimeListForSaveOrUpdate.addAll(minimumCoolingTimeRow);
        }
        minimumCoolingTimeService.saveOrUpdateBatch(minimumCoolingTimeListForSaveOrUpdate);
        return R.ok();
    }


    @ApiOperation(value = "根据是低燃耗还是高燃耗获取最小冷却时间列表")
    @GetMapping("/getMinimumCoolingTimeMap/{isLowBurnupOrHighBurnup}")
    public R getMinimumCoolingTimeMap(
            @ApiParam(name = "isLowBurnupOrHighBurnup", value = "是低燃耗还是高燃耗", required = true)
            @PathVariable Integer isLowBurnupOrHighBurnup){

        List<Range> burnupRangeList = rangeService.list(new QueryWrapper<Range>()
                .eq("is_low_burnup_or_high_burnup", isLowBurnupOrHighBurnup)
                .eq("is_burnup_or_initial_uranium_enrichment",0));

        List<Range> initialUraniumEnrichmentRangeList = rangeService.list(new QueryWrapper<Range>()
                .eq("is_low_burnup_or_high_burnup", isLowBurnupOrHighBurnup)
                .eq("is_burnup_or_initial_uranium_enrichment",1));

        List<MinimumCoolingTime> minimumCoolingTimeList = minimumCoolingTimeService.list(new QueryWrapper<MinimumCoolingTime>().eq("is_low_burnup_or_high_burnup", isLowBurnupOrHighBurnup));

        List<List<MinimumCoolingTime>> minimumCoolingTimeMatrix = new ArrayList<>();
        for (Range iueRange : initialUraniumEnrichmentRangeList) {
            List<MinimumCoolingTime> minimumCoolingTimeRow = new ArrayList<>();
            for (Range burnupRange : burnupRangeList) {
                MinimumCoolingTime initMinimumCoolingTime = new MinimumCoolingTime();
                initMinimumCoolingTime.setMinimumCoolingTimeValue(0D);
                initMinimumCoolingTime.setBurnupRangeId(burnupRange.getId());
                initMinimumCoolingTime.setInitialUraniumEnrichmentRangeId(iueRange.getId());
                initMinimumCoolingTime.setIsLowBurnupOrHighBurnup(isLowBurnupOrHighBurnup);

                for (MinimumCoolingTime minimumCoolingTime : minimumCoolingTimeList) {
                    if (minimumCoolingTime.getBurnupRangeId().equals(burnupRange.getId()) && minimumCoolingTime.getInitialUraniumEnrichmentRangeId().equals(iueRange.getId())) {
                        initMinimumCoolingTime = minimumCoolingTime;
                    }
                }
                minimumCoolingTimeRow.add(initMinimumCoolingTime);
            }
            minimumCoolingTimeMatrix.add(minimumCoolingTimeRow);
        }

        Map<String, Object> minimumCoolingTimeMap = new HashMap<>();  // 横轴、纵轴、数据
        minimumCoolingTimeMap.put("initialUraniumEnrichmentRangeList", initialUraniumEnrichmentRangeList);
        minimumCoolingTimeMap.put("burnupRangeList", burnupRangeList);
        minimumCoolingTimeMap.put("minimumCoolingTimeMatrix", minimumCoolingTimeMatrix);


        return R.ok().data("items", minimumCoolingTimeMap);
    }
}

